package cn.xgj.sfzy.service.impl;

import cn.xgj.sfzy.entity.Consult;
import cn.xgj.sfzy.entity.Field;
import cn.xgj.sfzy.mapper.ConsultMapper;
import cn.xgj.sfzy.mapper.FieldMapper;
import cn.xgj.sfzy.service.FieldService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 10:07
 * @Version 1.0
 */
@Service
public class FieldServiceImpl implements FieldService {

    @Autowired
    private FieldMapper fieldMapper;

    @Autowired
    private ConsultMapper consultMapper;

    @Override
    public Integer add(Field field) {
        return fieldMapper.insert(field);
    }

    @Override
    public Integer remove(Integer fieldId) {
        List<Consult> consults = consultMapper.selectByFieldId(fieldId);
        if(consults.size()!=0){
            return -1;
        }else{
            return fieldMapper.delete(fieldId);
        }
    }

    @Override
    public Integer edit(Field field) {
        return fieldMapper.update(field);
    }

    @Override
    public List<Field> findAll(Field field) {
        List<Field> fields = fieldMapper.selectAll(field);
        return fields;
    }

    @Override
    public Field findById(Integer fieldId) {
        return fieldMapper.selectById(fieldId);
    }

    @Override
    public PageInfo<Field> findByPage(Integer pageNum, Integer pageSize, Field field) {
        PageHelper.startPage(pageNum,pageSize);
        List<Field> fields = fieldMapper.selectAll(field);
        PageInfo<Field> pageInfo = new PageInfo<Field>(fields);

        return pageInfo;

    }
}
